<?php

namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\ArticleCategory;

class ArticlesController extends Controller
{
    public function index()
    {
        return view('index.articles.index');
    }

    public function show(Article $article)
    {
        //左侧导航
        $nav = $article->category->getNav();

        //面包屑
        $mbx['level_1']['url'] = $nav[0]['url'];
        if ($article->category->parent_id > 0) {
            //二级分类下的文章
            $mbx['level_1']['title'] = $article->category->parent->title;
            $mbx['level_2'] = ['title' => $article->category->title, 'url' => route('articles.category', ['article_category' => $article->category->id])];
        } else {
            //一级分类下的文章
            $mbx['level_1']['title'] = $article->category->title;
            $mbx['level_2'] = ['title' => $article->title, 'url' => route('articles.show', ['article' => $article->id])];
        }

        return view('index.articles.show', ['nav' => $nav, 'mbx' => $mbx, 'row' => $article]);
    }

    public function category(ArticleCategory $articleCategory)
    {
        //左侧导航
        $nav = $articleCategory->getNav();

        //面包屑
        $mbx['level_1']['url'] = $nav[0]['url'];
        //二级分类下的文章
        $mbx['level_1']['title'] = $articleCategory->parent->title;
        $mbx['level_2'] = ['title' => $articleCategory->title, 'url' => route('articles.category', ['article_category' => $articleCategory->id])];
        //数据分页
        $articles = Article::where('category_id', $articleCategory->id)
            ->orderBy('created_at', 'desc')
            ->paginate(2);
        return view('index.articles.show', ['list' => $articles, 'nav' => $nav, 'mbx' => $mbx, 'row' => $articleCategory]);
    }
}
